import { ElNotification, ElLoading } from 'element-plus'
const time = 2500
export const ToastSuccess = (message:any = '',duration=time) => {
    ElNotification({
        message,
        type: 'success',
        duration
    })
};
export const ToastWarning = (message:any = '',duration=time) => {
    ElNotification({
        message,
        type: 'warning',
        duration
    })
};
export const ToastError = (message:any = '',duration=time) => {
    ElNotification({
        message,
        type: 'error',
        duration
    })
}
export const Toastloading = (message = '加载中...') => {
    const loading = ElLoading.service({
        lock: true,
        text: message,
        background: 'rgba(0, 0, 0, 0.7)',
    })
    return loading;
}
export const allNumber = (row:any,key:any)=>{
    const val = row[key]
    row[key] = Number(val.replace(/[^\d]/g,"")); //只能输入数字
}
// vite中简写动态图片路径
export const getAssetsFile = (url: string) => {
    return new URL(`../assets/images/${url}`, import.meta.url).href
};

// 截取url后面字段
export const getUrlParams = function (key: any) {
    let url = window.location.href;
    const arr = url.split("?");
    const newArr = arr[1] ? arr[1].split("&") : [];
    for (var i = 0; i < newArr.length; i++) {
        let temp = newArr[i].split("=");
        if (temp[0] === key) {
            let search = temp[1];
            if (search.indexOf("#") > 0) {
                search = search.substring(0, search.indexOf("#"));
            }
            return search;
        }
    }
};
export const copyText = (e:string | number) => {
  if(!e) return
  let dataNum = e + ''
  var element = createElement(dataNum);
  element.select();
  element.setSelectionRange(0, element.value.length);
  document.execCommand("copy");
  element.remove();
  ToastSuccess("已复制到剪切板");
};
const createElement = (text = "") => {
  var isRTL = document.documentElement.getAttribute("dir") === "rtl";
  var element = document.createElement("textarea");
  // 防止在ios中产生缩放效果
  element.style.fontSize = "12pt";
  // 重置盒模型
  element.style.border = "0";
  element.style.padding = "0";
  element.style.margin = "0";
  // 将元素移到屏幕外
  element.style.position = "absolute";
  element.style[isRTL ? "right" : "left"] = "-9999px";
  // 移动元素到页面底部
  let yPosition = window.pageYOffset || document.documentElement.scrollTop;
  element.style.top = `${yPosition}px`;
  //设置元素只读
  element.setAttribute("readonly", "");
  element.value = text;
  document.body.appendChild(element);
  return element;
};
import router from '@/router/index'
export const $router = () => {
  return router
}